package com.raimcomputing.pickforme.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.raimcomputing.pickforme.common.exception.EmailAddressInUseException;
import com.raimcomputing.pickforme.common.utility.VoFactory;
import com.raimcomputing.pickforme.common.vo.UserVo;
import com.raimcomputing.pickforme.service.User;

public class AddUserFormController extends AbstractController {
    private final Log logger = LogFactory.getLog( getClass() );
    private User user = null;
    private ModelAndView mav = null;

    /**
     * Handles request from newUser.html
     */
    public ModelAndView handleRequestInternal( HttpServletRequest request,
            HttpServletResponse response ) throws ServletException, IOException {

        // Get the data required from the request
        String email = (String) request.getAttribute( "email" );
        String password = (String) request.getAttribute( "password" );
        String passwordConfirm = (String) request.getAttribute( "passwordConfirm" );
        
        //Check to make sure we got the correct data.
        if( email == null || password == null || passwordConfirm == null ) {
            logger.warn( "Missing required information." );
            mav = new ModelAndView( "addUserErrorPage" );
            mav.addObject( "email", email );
            mav.addObject( "error",
                    "Required information missing." );
            return(mav);
        }

        // Make sure that the passwords match
        if ( !password.equals( passwordConfirm ) ) {
            ModelAndView mav = new ModelAndView( "addUserErrorPage" );
            mav.addObject( "email", email );
            mav.addObject( "error", "Passwords do not match." );
        }

        // Create a new user
        UserVo newUser = (UserVo) VoFactory.getVo( UserVo.class );
        newUser.setEmail( email );

        boolean addressInUse = false;
        try {
            newUser = user.createUser( newUser, password );
        } catch ( EmailAddressInUseException e ) {
            logger.warn( "Email '" + email
                    + "' is already in use and may not be used for a new user" );
            addressInUse = true;
        }

        mav = null;
        if ( addressInUse ) {
            mav = new ModelAndView( "addUserErrorPage" );
            mav.addObject( "email", email );
            mav.addObject( "error",
                    "Email address is already in use and may not be used for a new user" );
        } else {
            mav = new ModelAndView( "addUserResponse" );
            mav.addObject( "email", newUser.getEmail() );
            mav.addObject( "id", newUser.getId() );
        }

        return (mav);
    }

    /**
     * Setter for the user service.
     * 
     * @param user
     *            The service to use.
     */
    public void setUser( User user ) {
        this.user = user;
    }
}
